//
//  JSPhotoView.m
//  NiubilityProject
//
//  Created by Apple on 2017/11/26.
//  Copyright © 2017年 Apple. All rights reserved.
//

#import "JSPhotoView.h"
#import "PhotoModel.h"
@interface JSPhotoView ()<SDPhotoBrowserDelegate>



@end


@implementation JSPhotoView

- (instancetype)init{
    if (self = [super init]) {
        self.gridView = [[QMUIGridView alloc] init];
        [self addSubview:self.gridView];
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}
- (void)setDataSource:(NSArray *)dataSource{
    _dataSource = dataSource;
    [self.gridView removeAllSubviews];
    NSInteger i = 0;
    for (id item in dataSource) {
        YYAnimatedImageView *imageV = [YYAnimatedImageView new];
        imageV.userInteractionEnabled = YES;
        imageV.contentMode = UIViewContentModeScaleAspectFill;
        imageV.layer.masksToBounds = YES;
        [self.gridView addSubview:imageV];
        if (ValidStr(item)) {
            NSString *path = (NSString *)item;
            [imageV setImageWithURL:[NSURL URLWithString:path] options:YYWebImageOptionProgressiveBlur];

        }else if ([item isKindOfClass:[PhotoModel class]]){
            PhotoModel *m = (PhotoModel *)item;
            [imageV setImageWithURL:[NSURL URLWithString:m.src] options:YYWebImageOptionProgressiveBlur];
        }
        imageV.tag = 1000 + i;
        i++;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(seeImage:)];
        [imageV addGestureRecognizer:tap];
    }
    
}
- (void)layoutSubviews{
    [super layoutSubviews];
    self.gridView.columnCount = 3;
    self.gridView.rowHeight = flat((CGRectGetWidth(self.bounds) - 2 *(kRealValue(10))) / self.gridView.columnCount);
    CGFloat gridViewHeight = [self.gridView sizeThatFits:CGSizeMake(CGRectGetWidth(self.bounds), CGFLOAT_MAX)].height;
    self.gridView.frame = CGRectMake(kRealValue(10), kRealValue(10), CGRectGetWidth(self.bounds)- 2 *(kRealValue(10)), gridViewHeight);
//    CGRect oldFrame = self.frame;
//    self.frame = CGRectMake(oldFrame.origin.x, oldFrame.origin.y, oldFrame.size.width, gridViewHeight + 2 *(kRealValue(10)));
    self.gridView.separatorWidth = kRealValue(10);
}
-(void) seeImage:(UITapGestureRecognizer *)sender {
    YYAnimatedImageView *imageV = (YYAnimatedImageView *)[sender view];
    SDPhotoBrowser *browwer = [[SDPhotoBrowser alloc] init];
    browwer.delegate = self;
    browwer.sourceImagesContainerView = self.gridView;
    browwer.currentImageIndex = imageV.tag - 1000;
    browwer.imageCount = self.dataSource.count;
    [browwer show];
}
- (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index{
    YYAnimatedImageView *imageV = [self viewWithTag:index + 1000];
    return imageV.image;
}
- (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index{
    NSString *url = nil;
    id item = self.dataSource[index];
    if ([item isKindOfClass:[NSString class]]) {
        url = (NSString *)item;
    }else if ([item isKindOfClass:[PhotoModel class]]){
        PhotoModel *m = (PhotoModel *)item;
        url = m.src;
    }
    return [NSURL URLWithString:url];
}
@end
